import { observable, action, toJS, computed } from 'mobx';
import { message } from 'antd';
import { WeaTableNew } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import * as Api from '../apis/groupvoting';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
const initalState = {
  title: '',
  newVisible: false,
  isShowDetail: false,
  listLoading: true,
  isSearch: false,
  dataInfo: {},
  selectedKeys: [],
  voteId: '',
  saveLoading: false,
  detailLoading: true,
  groupid: '',
  isgroup: '',
  listReloadLoading: false,
  checkedRowKeys: '',

  detailViewRight: false,

  personVisible: false,
  usertype: '',
  personTitle: '',
  optionid: '',
  personListLoading: false,
  isExistStore: false,
};
export default class ListStore {
  sessionkey = ''
  @observable groupListStore = new TableStore();
  @observable personListStore = new TableStore();
  @observable title = initalState.title;
  @observable newVisible = initalState.newVisible;
  @observable isShowDetail = initalState.isShowDetail;
  @observable isSearch = initalState.isSearch;
  @observable dataInfo = initalState.dataInfo;
  @observable selectedKeys = initalState.selectedKeys;
  @observable voteId = initalState.voteId;
  @observable saveLoading = initalState.saveLoading;
  @observable detailLoading = initalState.detailLoading;
  @observable groupid = initalState.groupid;
  @observable isgroup = initalState.isgroup;
  @observable listLoading = initalState.listLoading; // 列表加载状态
  @observable listReloadLoading = initalState.listReloadLoading;
  @observable checkedRowKeys = initalState.checkedRowKeys; // 标记选中左侧菜单的key


  // 详情
  @observable detailViewRight = initalState.detailViewRight;

  // 人员列表状态
  @observable personVisible = initalState.personVisible;
  @observable usertype = initalState.usertype;
  @observable personTitle = initalState.personTitle;
  @observable optionid = initalState.optionid; // 选项id
  @observable personListLoading = initalState.personListLoading;
  @observable isExistStore = initalState.isExistStore;

  @action setStoreState = (params, callback) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
    if (callback) callback();
  }

  /* 列表请求 */
  @action getGroupVoteList = (params, reloadPage = false, callback) => {
    if (reloadPage) {
      this.listReloadLoading = true;
    } else {
      this.listLoading = true;
    }
    params = {
      groupid: this.groupid,
      isgroup: this.isgroup,
      ...params,
    };
    Api.getGroupVoteList(params).then(
      action('fetchSuccess', (res) => {
        this.sessionkey = res.sessionkey;
        reloadPage ? this.getListData(res.sessionkey, '', '', '', () => {
          if (callback && typeof callback === 'function') callback();
          this.listReloadLoading = false;
        }) : this.getListData(res.sessionkey, 1, '', '', () => {
          if (callback && typeof callback === 'function') callback();
          this.listLoading = false;
        });
      }),
    );
  }

  @action getListData = (sessionkey, current, pageSize, sort, callback) => {
    if (!sessionkey) sessionkey = this.sessionkey;
    this.groupListStore.getDatas(sessionkey, current, pageSize, sort, () => {
      if (callback && typeof callback === 'function') callback();
    });
  }

  /* 删除群投票 */
  @action groupchatvotedel = (params) => {
    Api.groupchatvotedel(params).then((res) => {
      switch (res.api_status) {
        case 'success':
          this.getGroupVoteList('', true);
          message.success(getLabel('83472', '删除成功!'));
          break;
        case '0':
          message.error(getLabel('516185', '暂无权限删除！'));
          break;
        case '2':
          message.error(getLabel('516186', '该投票已删除！'));
          break;
        default:
          message.error(getLabel('384401', '删除失败，请重试！'));
      }
    });
  }

  /* 批量分享 */
  @action groupchatvoteshare = (params, callback) => {
    Api.groupchatvoteshare(params).then((res) => {
      if (res.api_status === '1') {
        message.success(getLabel('391211', '分享成功！'));
        this.groupListStore.setSelectedRowKeys([]);
        if (callback && typeof callback === 'function') callback(res);
      } else {
        message.error(getLabel('516187', '分享失败，请重试！'));
      }
    });
  }

  /* 详情 */
  @action viewGroupVote = (params) => {
    this.detailLoading = true;
    params = {
      groupid: this.groupid,
      votingid: this.voteId,
      ...params,
    };
    Api.viewGroupVote(params).then((res) => {
      this.detailViewRight = res.isExist === '1';
      if (res.isExist === '1') {
        let options = res.votedata && res.votedata.options;
        options = options.map(v => ({ ...v, checked: false }));
        let datas = {
          ...res.votedata,
          options,
        };
        this.dataInfo = datas;
      }
      this.detailLoading = false;
    });
  }

  // 投票
  @action onVote = (params, callback) => {
    let select = toJS(this.selectedKeys);
    let data = toJS(this.dataInfo);
    if (select.length === 0) {
      message.error(getLabel('516188', '请选择！'));
      return;
    }

    if (data.maxvoteoption !== '0') {
      if (data.choosemodel === '1' && (Number(data.maxvoteoption) < select.length)) {
        message.error(getLabel('516189', '您已超出最大可选项数！'));
        return;
      }
    }

    this.saveLoading = true;
    params = {
      votingid: this.voteId,
      groupid: this.groupid,
      votooptioncheck: select.join(),
      ...params,
    };
    Api.savegroupvoteresult(params).then((res) => {
      if (res.status === '1') {
        message.success(getLabel('516190', '投票成功！'));
        this.viewGroupVote();
        if (callback && typeof callback === 'function') callback(res);
      }
      this.selectedKeys = [];
      this.saveLoading = false;
    });
  }

  /** **********************************   投票人员--列表   *********************************** */
  @action groupchatvoteprelist = (params) => {
    this.personListLoading = true;
    params = {
      votingid: this.voteId,
      usertype: this.usertype,
      groupid: this.groupid,
      ...params,
    };
    if (this.optionid) {
      params = { ...params, optionid: this.optionid };
    }
    Api.groupchatvoteprelist(params).then((res) => {
      this.personListStore.getDatas(res.sessionkey, 1);
      this.isExistStore = true;
      this.personListLoading = false;
    });
  }
}
